/**
 * 
 */

package com.chengyu.eyc.utils;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * @author kevin
 *
 */
@Component
public final class RedisUtil {

    private static RedisTemplate<String, Object> redisTemplate;

    /**
     * @param accessTokenKey
     * @return
     */
    public static boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    public static Object get(String key) {
        ValueOperations<String, Object> strOps = redisTemplate.opsForValue();
        return strOps.get(key);
    }

    public static void set(String key, Object value) {
        ValueOperations<String, Object> strOps = redisTemplate.opsForValue();
        strOps.set(key, value);
    }

    public static void set(String key, Object value, long timeout) {
        ValueOperations<String, Object> strOps = redisTemplate.opsForValue();
        strOps.set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * @param redisTemplate the redisTemplate to set
     */
    @Resource
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

}
